

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="一又四分之一">
  <meta name="keywords" content="">
  
    <meta name="description" content="API 1.变量的定义能用 const 就用 Q：怎么考虑能不能用？ A：复杂数据可以用const ，原理？？？不建议看 123&#x2F;*		因为复杂数据类型的值是放到堆中的，在栈中的只是该内容的地址，那么修改时，是在堆中修改的，栈中的地址不会改变，所以等同于数组或者对象等没有改变；但是如果直接指向一个新的数组或对象等，那么就不能用 const 	     2.DOM（文档对象模型）是什么：用来呈现以及">
<meta property="og:type" content="article">
<meta property="og:title" content="js API总结">
<meta property="og:url" content="https://one-and-one-fourth.gitee.io/2024/02/03/js_API/index.html">
<meta property="og:site_name" content="one-and-one-fourth">
<meta property="og:description" content="API 1.变量的定义能用 const 就用 Q：怎么考虑能不能用？ A：复杂数据可以用const ，原理？？？不建议看 123&#x2F;*		因为复杂数据类型的值是放到堆中的，在栈中的只是该内容的地址，那么修改时，是在堆中修改的，栈中的地址不会改变，所以等同于数组或者对象等没有改变；但是如果直接指向一个新的数组或对象等，那么就不能用 const 	     2.DOM（文档对象模型）是什么：用来呈现以及">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-02-03T10:02:00.000Z">
<meta property="article:modified_time" content="2024-02-15T02:28:20.864Z">
<meta property="article:author" content="一又四分之一">
<meta property="article:tag" content="js">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>js API总结 - one-and-one-fourth</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"one-and-one-fourth.gitee.io","root":"/","version":"1.9.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  

  

  
    <!-- Google tag (gtag.js) -->
    <script async>
      if (!Fluid.ctx.dnt) {
        Fluid.utils.createScript("https://www.googletagmanager.com/gtag/js?id=", function() {
          window.dataLayer = window.dataLayer || [];
          function gtag() {
            dataLayer.push(arguments);
          }
          gtag('js', new Date());
          gtag('config', '');
        });
      }
    </script>
  

  

  

  

  



  
<meta name="generator" content="Hexo 7.1.1"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>one-and-one-fourth</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="js API总结"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2024-02-03 18:02" pubdate>
          2024年2月3日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          1.3k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          11 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">js API总结</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="API"><a href="#API" class="headerlink" title="API"></a>API</h1><hr>
<h2 id="1-变量的定义"><a href="#1-变量的定义" class="headerlink" title="1.变量的定义"></a>1.变量的定义</h2><p>能用 <code>const</code> 就用</p>
<p>Q：怎么考虑能不能用？</p>
<p>A：复杂数据可以用<strong>const</strong> ，原理？？？不建议看</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">/*	</span><br><span class="hljs-comment">	因为复杂数据类型的值是放到堆中的，在栈中的只是该内容的地址，那么修改时，是在堆中修改的，栈中的地址不会改变，所以等同于数组或者对象等没有改变；但是如果直接指向一个新的数组或对象等，那么就不能用 const </span><br><span class="hljs-comment">	</span><br></code></pre></td></tr></table></figure>



<hr>
<h2 id="2-DOM（文档对象模型）"><a href="#2-DOM（文档对象模型）" class="headerlink" title="2.DOM（文档对象模型）"></a>2.DOM（文档对象模型）</h2><p>是什么：用来呈现以及任意 HTML 或 XML  文档交互的API	</p>
<p>作用：开发网页内容实现用户交互</p>
<h3 id="1-DOM-树"><a href="#1-DOM-树" class="headerlink" title="1.DOM 树"></a>1.DOM 树</h3><p>​	是：将文档以树状结构直观的表现出来的，又称文档树；是描述网页内容关系的名词</p>
<p>​	作用：直观体现了标签与标签之间的关系</p>
<h3 id="2-DOM对象"><a href="#2-DOM对象" class="headerlink" title="2.DOM对象"></a>2.DOM对象</h3><p>是：浏览器根据 html  标签生成的 <strong>JS对象</strong></p>
<ul>
<li>所有的标签属性都可以在这个对象上找到</li>
<li>修改这个对象的属性会自动映射到标签的身上</li>
</ul>
<p><strong>document</strong> 对象：</p>
<ul>
<li>他所提供的属性和方法都是用来访问和操作网页内容的</li>
<li>网页所有的内容都在 document 中</li>
<li>是 DOM 里提供的一个对象</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> nav=<span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;css选择器&#x27;</span>)<br><span class="hljs-comment">//将网页内的第一个所选择的css选择器对象 赋值 给 nav ，此时nav是一个 对象</span><br><br><span class="hljs-comment">// 注意选择的是第一个，</span><br><br><span class="hljs-comment">//例如：</span><br>&lt;div&gt;<span class="hljs-number">123</span>&lt;/div&gt;<br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>456<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span><br><span class="hljs-keyword">const</span> nav=<span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;div&#x27;</span>)<br><span class="hljs-comment">/*	选择的是 123 那个 div</span><br><span class="hljs-comment"></span><br></code></pre></td></tr></table></figure>



<p>获取的样例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;ul&gt;<br>    <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span><br>	<span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span><br>	<span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span><br>&lt;/ul&gt;<br><br><span class="hljs-keyword">const</span> l=<span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;ul li:first-child&#x27;</span>)<br><span class="hljs-comment">//会只获取第一个 li </span><br><span class="hljs-comment">// 额。。。。不写  &#x27;:first-fhild&#x27; 也会只去第一个 li </span><br><span class="hljs-comment">//  :nth-child(n)   选择第n个 </span><br><span class="hljs-comment">//取所有的用</span><br><span class="hljs-keyword">const</span> l=<span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelectorAll</span>(<span class="hljs-string">&#x27;ul li&#x27;</span>)<br><span class="hljs-comment">//得到的是一个伪数组，pop(), push() 等不能用，但是可以遍历，获取长度</span><br><br></code></pre></td></tr></table></figure>



<h3 id="3-修改对象内容"><a href="#3-修改对象内容" class="headerlink" title="3.修改对象内容"></a>3.修改对象内容</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs JS"><span class="hljs-comment">// 页面的内容</span><br><span class="hljs-number">1.</span> 对象名.<span class="hljs-property">innerText</span>=<span class="hljs-string">&#x27;修改内容&#x27;</span><br><span class="hljs-comment">// 不会解析修改内容里面的标签</span><br><br><span class="hljs-number">2.</span> 对象名.<span class="hljs-property">innerHTML</span>=<span class="hljs-string">&#x27;修改内容&#x27;</span><br><span class="hljs-comment">// 会解析标签</span><br></code></pre></td></tr></table></figure>

<h3 id="4-删除数组的元素"><a href="#4-删除数组的元素" class="headerlink" title="4.删除数组的元素"></a>4.删除数组的元素</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js">数组名.<span class="hljs-title function_">splice</span>(开始的下标，删除几个)<span class="hljs-comment">//包括开开始的标签</span><br></code></pre></td></tr></table></figure>



<h3 id="5-操作元素的属性"><a href="#5-操作元素的属性" class="headerlink" title="5.操作元素的属性"></a>5.操作元素的属性</h3><h4 id="（1）常用属性"><a href="#（1）常用属性" class="headerlink" title="（1）常用属性"></a>（1）常用属性</h4><ul>
<li>src  ， herf  等</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js">对象.属性 = 值<br><span class="hljs-comment">//修改的方式和对象一样，没有创建，有的修改</span><br></code></pre></td></tr></table></figure>

<h4 id="（2）元素的样式属性"><a href="#（2）元素的样式属性" class="headerlink" title="（2）元素的样式属性"></a>（2）元素的<strong>样式</strong>属性</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs js">对象名.<span class="hljs-property">style</span>.样式属性 = 值<br><br><span class="hljs-comment">//如：</span><br>	&lt;script&gt;<br>        <span class="hljs-keyword">const</span> box=<span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;div&#x27;</span>)<span class="hljs-comment">//获取div</span><br><span class="hljs-comment">//修改宽 背景颜色 外边框</span><br>        box.<span class="hljs-property">style</span>.<span class="hljs-property">width</span>=<span class="hljs-string">&#x27;400px&#x27;</span><br>        box.<span class="hljs-property">style</span>.<span class="hljs-property">backgroundColor</span>=<span class="hljs-string">&#x27;red&#x27;</span><br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(box)<br>        box.<span class="hljs-property">style</span>.<span class="hljs-property">border</span>=<span class="hljs-string">&#x27;2px solid green&#x27;</span><br>    &lt;/script&gt;<br></code></pre></td></tr></table></figure>



<p><strong>特例</strong>：</p>
<p>因为 body 是唯一的标签，所以改标签不用获取，直接修改即可</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable language_">document</span>.<span class="hljs-property">body</span>.<span class="hljs-property">style</span>.样式属性 = 值<br></code></pre></td></tr></table></figure>

<h4 id="（3）操作类名"><a href="#（3）操作类名" class="headerlink" title="（3）操作类名"></a>（3）操作类名</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js">元素.<span class="hljs-property">className</span> = <span class="hljs-string">&#x27;类名1...&#x27;</span><br><span class="hljs-comment">//用新的类名替换旧的类名 ，类名替换元素 ， 元素是获取的类名，可以用过个类名替换原来的类名</span><br><br><span class="hljs-comment">//用下面的代码做解释</span><br>box.<span class="hljs-property">className</span> = <span class="hljs-string">&#x27;avtive&#x27;</span><span class="hljs-comment">//会实现将 .opa 替换掉</span><br>box.<span class="hljs-property">className</span> = <span class="hljs-string">&#x27;opa active&#x27;</span><span class="hljs-comment">//添加多个类名，替换 .opa </span><br></code></pre></td></tr></table></figure>



<p>为了解决 <code>className</code> 容易覆盖类名的问题，衍生出了 <code>classList</code> </p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">        <span class="hljs-selector-class">.opa</span>&#123;</span><br><span class="language-css">            <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;<span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;</span><br><span class="language-css">            <span class="hljs-attribute">background-color</span>: black;</span><br><span class="language-css">        &#125;</span><br><span class="language-css">        <span class="hljs-selector-class">.active</span>&#123;</span><br><span class="language-css">            <span class="hljs-attribute">color</span>: red;</span><br><span class="language-css">            <span class="hljs-attribute">background-color</span>: skyblue;</span><br><span class="language-css">        &#125;</span><br><span class="language-css">    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;opa&quot;</span>&gt;</span><br>        文字颜色<br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript">        <span class="hljs-keyword">const</span> box=<span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;.opa&#x27;</span>)<span class="hljs-comment">//类名必须写成相应的格式</span></span><br><span class="language-javascript">        box.<span class="hljs-property">classList</span>.<span class="hljs-title function_">add</span>(<span class="hljs-string">&#x27;active&#x27;</span>)<span class="hljs-comment">//添加一个类</span></span><br><span class="language-javascript">        box.<span class="hljs-property">classList</span>.<span class="hljs-title function_">remove</span>(<span class="hljs-string">&#x27;opa&#x27;</span>)<span class="hljs-comment">//删除一个类</span></span><br><span class="language-javascript">        box.<span class="hljs-property">classList</span>.<span class="hljs-title function_">toggle</span>(<span class="hljs-string">&#x27;opa&#x27;</span>)<span class="hljs-comment">//有这个类就删除，没有就添加</span></span><br><span class="language-javascript">       </span><br><span class="language-javascript"><span class="hljs-comment">//最终显示的背景颜色是 .active 中的，添加 .opa 并没有覆盖前者，是因为前者距离 body 比较远</span></span><br><span class="language-javascript"></span><br><span class="language-javascript">    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h3 id="6-setInterval-函数，间隔时间"><a href="#6-setInterval-函数，间隔时间" class="headerlink" title="6.setInterval ( 函数，间隔时间 )"></a>6.setInterval ( 函数，间隔时间 )</h3><p>默认时间单位 ms</p>
<p>函数不能写成调用的格式，比如：有名函数 fn()  ，写的时候不能加 后面的小括号，立即执行函数也是，不能加后面的小括号，除非用引号的情况</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">let</span> n=<span class="hljs-built_in">setInterval</span>(<span class="hljs-string">&#x27;fn()&#x27;</span>,<span class="hljs-number">1000</span>)<br><span class="hljs-comment">//这种的可以加小括号</span><br><span class="hljs-comment">//必须有变量名才能关</span><br><span class="hljs-built_in">clearInterval</span>(n)<span class="hljs-comment">//关闭定时器</span><br></code></pre></td></tr></table></figure>

<h3 id="7-事件监听"><a href="#7-事件监听" class="headerlink" title="7.事件监听"></a>7.事件监听</h3><blockquote>
<p>事件：在编程是系统内发生的动作或发生的事情</p>
<p>比如：在网页上点击一个按钮</p>
</blockquote>
<blockquote>
<p>让程序检测是否有事件发生，一旦有事件触发，就立即调用一个函数做出响应，也称 绑定事件 或 注册事件</p>
<p>比如：点击可以播放轮播图</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js">元素对象.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">&#x27;事件类型&#x27;</span>,要执行的函数)<br></code></pre></td></tr></table></figure>

<p>三要素：</p>
<ul>
<li>事件源：那个dom元素被时间触发了，要获取dom元素</li>
<li>事件类型：用什么方式触发，比如：单击（click），鼠标经过（mouseenter），鼠标离开（mouseleave）</li>
<li>事件调用函数：要做什么事情</li>
</ul>
<hr>
<h2 id="3-BOM-浏览器对象模型"><a href="#3-BOM-浏览器对象模型" class="headerlink" title="3.BOM (浏览器对象模型)"></a>3.BOM (浏览器对象模型)</h2><p>作用：使用 <strong>js</strong> 去操作 <strong>html</strong> 和 <strong>浏览器</strong> </p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/html/" class="category-chain-item">html</a>
  
  
    <span>></span>
    
  <a href="/categories/html/Js/" class="category-chain-item">Js</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/js/" class="print-no-link">#js</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>js API总结</div>
      <div>https://one-and-one-fourth.gitee.io/2024/02/03/js_API/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>一又四分之一</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2024年2月3日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2024/02/03/hexo%E6%9B%B4%E6%96%B0%E6%B5%81%E7%A8%8B%E5%8F%8A%E8%A7%A3%E6%9E%90/" title="hexo更新流程及解析">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">hexo更新流程及解析</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2024/02/03/hello-world/" title="Hello World">
                        <span class="hidden-mobile">Hello World</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
